package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.miaodong.entity.AppMounts;
import com.vf.admin.miaodong.service.AfterSystemService;
import com.vf.admin.utils.Base64Image.FileUtil;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * 系统通息
 *
 * @author anxiang
 */
@CrossOrigin
@Controller
@RequestMapping("/after/system")
public class AfterSystemController extends BaseController {

    @Autowired
    AfterSystemService afterSystemService;

    /**
     * 获取系统消息
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean) {
        try {
            Map<String, Object> map = new HashMap<>(16);
            List<AppMessageRecord> appMessageRecords = afterSystemService.findList(map, splitPageBean);
            request.setAttribute("list", appMessageRecords);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/system/list");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    /**
     * 系统消息添加跳转
     *
     * @return
     */
    @RequestMapping("/add")
    public String add() {
        try {
            return this.sucess("/web/after/system/add");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    /**
     * 系统消息编辑跳转
     *
     * @param id id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppMessageRecord appMessageRecord = afterSystemService.get(id);
            request.setAttribute("appMessageRecord", appMessageRecord);
            return this.sucess("/web/after/system/edit");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 添加系统消息
     *
     * @param appMessageRecord 实体类
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result addMessage(AppMessageRecord appMessageRecord) {
        try {
//            System.err.println("jinlaile");
            if (StringUtils.isEmpty(appMessageRecord.getMessage())) {
                return errorJSON("消息不能为空");
            }
            String filePath = "";
            if(!isNull(appMessageRecord.getImage())) {
                Map map = FileUtil.aliyunOSSUpload(appMessageRecord.getImage(), "");
                if (map.get("st").equals("0")) {
                    filePath = (String) map.get("data");
                }
                if (StringEx.isNotNull(filePath)) {
                    appMessageRecord.setImage(filePath);
                }
            }
            appMessageRecord.setMessage(appMessageRecord.getMessage());
            appMessageRecord.setTitle(appMessageRecord.getTitle());
            appMessageRecord.setCreatetime(new Date());
            appMessageRecord.setLeixing(1);
            appMessageRecord.setState(appMessageRecord.getState() == null ? Constants.shiFou_STATE_FOU : appMessageRecord.getState());
            afterSystemService.save(appMessageRecord);
            return sucessJSON("添加成功");
        } catch (Exception e) {
            System.out.println(e);
            return this.errorJSON("系统繁忙");
        }
    }


    /**
     * 删除系统消息
     * @param appMessageRecord
     * @return
     */
    @RequestMapping("/delMessage")
    @ResponseBody
    public Result delMessage(AppMessageRecord appMessageRecord)
    {
        try {
            if(StringEx.isNullOrLing(appMessageRecord.getId())){
                return this.errorJSON("参数错误");
            }
            afterSystemService.del(appMessageRecord.getId());
            return sucessJSON("删除成功");
        }catch (Exception e){
            return this.errorJSON("系统繁忙");
        }
    }



}
